
from app.services.doubao.interface import UBALDOInterface
from app.services.doubao.model_registry import register_model


@register_model("text")
class TextBasedUBALDO(UBALDOInterface):
    def recognize(self, file_path: str, full_text: str = "") -> dict:
        from app.services.tenant_config_service import get_tenant_config
        from openai import OpenAI
        import json
        from app.config import exConfig

        tenant_config = get_tenant_config().get_doubao_ai_config()

        prompt = f"""
           请将以下驾考题目解析为如下 JSON 格式，仅输出 JSON，不加任何说明或解释：
           [
              {{
                  "type_text": "题目类型",
                  "subject": "题目内容",
                  "answer": "正确选项的序号（1 表示 A，2 表示 B，3 表示 C，4 表示 D）",
                  "optionA": "选项A内容",
                  "optionB": "选项B内容",
                  "optionC": "选项C内容",
                  "optionD": "选项D内容",
                  "solve": "题目解析 简洁明了"
              }}
           ]
           题目如下：
           {full_text}
           """

        client = OpenAI(
            base_url="https://ark.cn-beijing.volces.com/api/v3",
            api_key=tenant_config.get('api_key', exConfig.DOUBAO_API_KEY),
        )

        try:
            response = client.chat.completions.create(
                model=tenant_config.get('model', exConfig.DOUBAO_MODEL_ID),
                messages=[
                    {"role": "system", "content": "你是一个驾考题库专家"},
                    {"role": "user", "content": prompt}
                ],
                stream=False
            )

            return json.loads(response.choices[0].message.content)
        except Exception as e:
            print("❌ 文本识别失败：", e)
            return {}



